package com.lkd.handler;

import com.google.common.base.Strings;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.contract.SupplyContract;
import com.lkd.contract.VmStatusContract;
import com.lkd.emq.Topic;
import com.lkd.entity.TaskDetailsEntity;
import com.lkd.feign.VMService;
import com.lkd.http.vo.TaskViewModel;
import com.lkd.service.TaskDetailsService;
import com.lkd.service.TaskService;
import com.lkd.utils.JsonUtil;
import com.lkd.vo.VmVO;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

/**
 * @Author FJL
 * @Description TODO
 * @Date 2022/8/28 10:01
 **/
@Topic("")
public class VMServiceHandler implements MsgHandler {

    @Autowired
    private VMService vmService;

@Autowired
private TaskService taskService;
    @Override
    public void process(String jsonMsg) throws IOException {
        // 将json字符串解析成supplyContract
        VmStatusContract vmStatusContract = JsonUtil.getByJson(jsonMsg, VmStatusContract.class);
        // 参数校验
        if(vmStatusContract==null || Strings.isNullOrEmpty(vmStatusContract.getInnerCode())) return;

        // 判断是否创建维修工单，即status是否为false,状态不正常
       if( vmStatusContract.getStatusInfo().stream().anyMatch(s->s.isStatus()==false)){
           VmVO vmInfo = vmService.getVMInfo(vmStatusContract.getInnerCode());
           if(vmInfo==null){
               return;
           }
           Integer userId = taskService.getLeastTask(vmInfo.getRegionId(), true); // 存储到redis中
           if(userId!=0){
               TaskViewModel task = new TaskViewModel();
               task.setCreateType(0);
               task.setInnerCode(vmStatusContract.getInnerCode());
               task.setUserId(userId);
               task.setProductType(VMSystem.TASK_TYPE_REPAIR);
               task.setDesc(jsonMsg);
taskService.createTask(task);
           }


       }
    }
   }
